/*
 * sched.h - Estructures i macros pel tractament de processos
 */

#include <list.h>
#include <mm_address.h>

#ifndef __SCHED_H__
#define __SCHED_H__

#define NR_TASKS      10
//file descriptors
#define NR_FD 10

#define KERNEL_STACK_SIZE	1024

#define RUN 0
#define READY 1
#define BLOCKED 2
#define FREE -1

#define QUANTUM 10
//semafores
#define NR_SEM 25


//structs

struct file_descriptor
{
	char name[10];
	unsigned int * file_accessed;
};

struct semaphore { 
 	int owner;		//owner of the process
	int counter;		//counter
	int waiting;		//processes in queue	
    	struct list_head sem_list;
};

struct semaphore sem[NR_SEM];


/*** Global variables */
int pid;	//increments with creation of every new process 
int current_quantum;	//shows how many tics have left for the current process
int switch_task_by_interruption;	//a flag for EOI
struct list_head runqueue;


struct task_struct {
	int pid;		//process identifier
	int state;	//process state
	struct list_head list; //list
	unsigned int frames[NUM_PAG_DATA];
	int quantum;
			//variables for stats prouposes
	int total_trans;	//total transitions to RUN
	int total_tics;		//total tics executed by the process

	//file descriptors	
	struct file_descriptor fd[NR_FD];

	
};

union task_union {
  struct task_struct task;
  unsigned long stack[KERNEL_STACK_SIZE];    /* pila de sistema, per procés */
};

struct protected_task_struct {
  unsigned long task_protect[KERNEL_STACK_SIZE];  /* This field protects the different task_structs, so any acces to this field will generate a PAGE FAULT exeption */
  union task_union t;
};

extern struct protected_task_struct task[NR_TASKS];

#define KERNEL_ESP       (DWord) &task[0].t.stack[KERNEL_STACK_SIZE]

/*** Functions */
struct task_struct * current();

struct task_struct * list_head_to_task_struct(struct list_head *l);

int find_free_task();

void scheduling();

void round_robin();

void task_switch(union task_union * t);

void sem_config(int n_sem);

int sem_valid(int n_sem);


/* Inicialitza les dades del proces inicial */
void init_task0(void);

#endif  /* __SCHED_H__ */
